<template>
	<view class="the-page">
		<template v-if="cartList.length == 0">
			<image src="/static/image/shopcart_empty.png" style="width:100%;"></image>
		</template>
		<template v-else>
			<!-- 全选 清空  -->
			<view class="cart_header">
				<view>
					<!-- 全选 -->
					<checkbox :checked="isCheckedAll" @click="handleSelectAll">全选</checkbox>
				</view>
				<view class="button-delete-all" @click="handleDeleteAll">
					<uni-icons type="trash" :size="18" />
					<span>清空</span>
				</view>
			</view>
			<!-- 商品列表区域 -->
			<view class="cart-list">
				<checkbox-group placement="column">
					<view class="cart-item" v-for="(item, i) in cartList" :key="i">
						<view class="cart-check">
							<!-- 复选框 -->
							<checkbox :checked="item.isChecked " @click="handleCheckedChange(item)" />
						</view>

						<view class="cart-img">
							<!-- 中间商品图片 -->
							<image :src="baseUrl + item.goodsImg" style="width:80px;height:80px;" />
						</view>

						<view class="cart-other">
							<view class="goods-name">{{ item.goodsName }}</view>
							<view class="price-operation">
								<view class="price">¥{{ item.goodsPrice }}</view>
								<view class="operation">
									<view style="display: flex;flex-direction: row;">
										<uni-number-box v-model="item.count" @change="handleCountChanged(item)" :min="1" :max="10" />
										<button size="mini" type="primary" @click="handleDeleteCartItem(item.id)">删除</button>
									</view>
								</view>
							</view>
						</view>
					</view>
				</checkbox-group>
			</view>

			<!-- 商品结算区域 -->
			<uni-list-item title="已选商品总金额">
				<!-- 自定义 footer-->
				<template v-slot:footer>
					<span class="red">¥{{ getCheckedAmount }}</span>
				</template>
			</uni-list-item>

			<!-- 结算按钮 -->
			<button type="primary" size="large" @click="handleCreateOrder">去结算</button>
		</template>
	</view>
</template>

<script>
	// 导入配置的基地址
	import {
		baseUrl
	} from "@/utils/config.js"
	
	//导入接口
	import * as cartAPI from '@/api/cart.js';

	export default {
		name: 'Cart',
		data() {
			return {
				cartList: [], //购物车列表
				isCheckedAll: false, //是否全选
			};
		},

		//实例的生命周期函数，实例创建后被调用
		onLoad() {
			this.getCartList(); //初始化购物车数据
		},
		onShow() {
			this.getCartList(); //初始化购物车数据
		},

		computed: {
			getCheckedCount() { //计算勾选记录总数
				let sum = 0;
				this.cartList.forEach((item) => {
					if (item.isChecked) sum += 1;
				});
				return sum;
			},
			baseUrl(){
				return baseUrl
			},
			//计算是否全部勾选
			getCheckedAll() {
				return this.cartList.length == this.getCheckedCount ? true : false;
			},

			//统计购物车金额
			getCheckedAmount() {
				let amount = 0;
				this.cartList.forEach((item) => {
					if (item.isChecked) amount += item.goodsPrice * item.count;
				});
				return amount;
			},
		},

		methods: {
			//获取用户购物车数据
			getCartList() {
				this.cartList = []; //清空
				cartAPI.getMyList().then(result => {
					this.cartList = result.data;
					this.isCheckedAll = this.getCheckedAll; //同步是否全选
				}).catch(() => {});
			},

			//触发更改购物商品选中状态
			handleCheckedChange(item) {
				item.isChecked = !item.isChecked;
				let id = item.id;
				let isChecked = item.isChecked;
				let params = {
					id,
					isChecked
				};
				cartAPI.update(params).then(() => {
					this.isCheckedAll = this.getCheckedAll; //同步是否全选
				}).catch(() => {});
			},

			//触发更改购物商品数量,回调参数见官网文档
			handleCountChanged(item) {
				let params = {
					id: item.id,
					count: item.count
				};
				cartAPI.update(params).then(() => {});
			},

			//触发删除购物商品
			handleDeleteCartItem(id) {
				let ids = [];
				ids.push(id);
				cartAPI.deleteByIds(ids).then(() => {
					this.getCartList(); //刷新列表数据
				});
			},

			//触发全选/全不选
			handleSelectAll() {
				//准备参数
				this.isCheckedAll = !this.isCheckedAll;
				let isChecked = this.isCheckedAll;
				//调用接口
				cartAPI.updateIsCheckedAllByMy(isChecked).then(() => {
					this.getCartList(); //刷新列表数据
				});
			},

			//触发清空
			handleDeleteAll() {
				uni.showModal({
					title: '提示',
					content: '客官，您确定清空购物车吗?',
					success: result => {
						if (result.confirm) {
							cartAPI.deleteAllByMy().then(() => {
								this.getCartList(); //刷新列表数据
							});
						}
					}
				});
			},

			//触发创建订单
			handleCreateOrder() {
				if (this.cartList.length === 0) {
					uni.showToast({ title: '您的购物车为空', icon: 'error' });
					return;
				}
				// uni.showToast({ title: '功能还没实现', icon: 'exception' });
				// return
				uni.navigateTo({
					url: '/pages/order/orderCreate'
				});
			}
		}
	};
</script>

<style lang="scss" scoped="scoped">
	.the-page {
		background-color: white;

		.cart_header {
			padding: 10px;
			display: flex;
			flex-direction: row;
			justify-content: space-between;
			align-items: center;
			border-bottom: 1px solid lightgrey;
			font-size: 14px;
			line-height: 30px;
		}
	}

	.button-delete-all {
		display: flex;
		align-items: center;
	}

	.cart-list {
		padding: 10px;
	}

	.cart-item {
		padding: 5px 0;
		display: flex;
		flex-direction: row;
		justify-content: space-between;
		align-items: center;
	}

	.cart-check {
		width: 50px;
	}

	.cart-img {
		width: 100px;
	}

	.cart-other {
		width: 60%;
		display: flex;
		flex-direction: column;
		flex: 1;
	}

	.goods-name {
		padding: 5px 10px;
		font-size: 12px;
	}

	.price-operation {
		display: flex;
		flex-direction: row;
		justify-content: space-between;
	}

	.price {
		padding-left: 10px;
		text-align: left;
		font-size: 14px;
		font-weight: bold;
		color: red;
		display: flex;
		align-items: center;
	}

	.operation {
		padding-right: 10px;
		display: flex;
		flex-direction: row;
		align-items: center;
		justify-content: flex-end;

		::v-deep .uni-numbox__value {
			height: 100%;
		}
	}

	.red {
		color: red;
		font-weight: bold;
		font-size: 14px;
	}
</style>